﻿using System.Collections.Generic;
namespace GX
{
    public partial class DecisionTree
    {
        public abstract class DTCondition
        {
            DTConditionContainer _parent;
            bool _isNegative = false;
            public bool isNegative
            {
                get
                {
                    return _isNegative;
                }
                set
                {
                    _isNegative = value;
                }
            }
            public DTConditionContainer parent
            {
                get { return _parent; }
                internal set { _parent = value; }
            }
            public bool Evaluate()
            {
                if (isNegative)
                    return !DoEvaluate();
                return DoEvaluate();
            }
            abstract protected bool DoEvaluate();
        }



        public abstract class DTConditionContainer : DTCondition
        {
            List<DTCondition> _children;
            public int numChildren { get { return null == _children ? 0 : _children.Count; } }
            public void AddChild(DTCondition node)
            {
                if (node == this || node.parent == this) return;

                var p = this.parent;
                while (null != p)
                {
                    if (node == p) return;
                    p = p.parent;
                }

                if (null == _children)
                {
                    _children = new List<DTCondition>();
                }
                _children.Add(node);
                node.parent = this;
            }
            public void RemoveChild(DTCondition node)
            {
                int index = GetChildIndex(node);
                if (index == -1) return;
                _children.RemoveAt(index);
                node.parent = null;
            }
            public int GetChildIndex(DTCondition node)
            {
                if (node.parent != this || null == _children) return -1;
                return _children.IndexOf(node);
            }
            public DTCondition GetChildAt(int index)
            {
                return null == _children ? null : _children[index];
            }
        }
    }
}